package com.example.yyg.springframework.beans.factory.support;

import com.example.yyg.springframework.beans.factory.BeanFactory;
import com.example.yyg.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.util.Objects;

public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName, args);
    }

    //todo.recode 范型方法的使用 .<A> doGetBean()
    public <T> T doGetBean(String beanName, Object[] args){
        //todo.think 得到bean一定是符合要求的，是args参数生成的吗
        T bean = (T) getSingleton(beanName);
        if (bean!=null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        bean = (T) createBean(beanName, beanDefinition, args);
        return bean;
    }

    //todo.think 用哪个构建方法是这个内部的事
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition,  Object[] args);

    protected abstract BeanDefinition getBeanDefinition(String beanName);


}
